import threading
from typing import List
from .job_store.job import Job


class BucketDataStruct:
    """时间格数据结构"""
    bucket_idx: int  # 时间格ID
    expiration: int  # 任务过期时间, 单位毫秒
    wheel_id: int  # buckets所属的时间轮ID
    timers: List[Job]
    lock: threading.Lock

    def __init__(self, expiration, timers, bucket_idx: int, wheel_id: int):
        self.expiration = expiration
        self.timers = timers
        self.bucket_idx = bucket_idx
        self.wheel_id = wheel_id
        self.lock = threading.Lock()


class TimingWheelDataStruct:
    """时间轮数据结构"""
    # 时间跨度, 单位毫秒
    tick: int
    # 时间轮格数
    wheel_size: int
    # 时间轮总跨度, 单位毫秒
    interval: int
    # 时间轮所处的当前时间, 单位毫秒
    current_time: int
    # 时间格列表
    bucket_idxs: List[int]
    # 上层时间轮ID
    overflow_wheel_id: int
    lock: threading.Lock


    def __init__(self,
                 tick,
                 wheel_size,
                 interval,
                 current_time,
                 bucket_idxs,
                 overflow_wheel_id):
        self.tick = tick
        self.wheel_size = wheel_size
        self.interval = interval
        self.current_time = current_time
        self.bucket_idxs = bucket_idxs
        self.overflow_wheel_id = overflow_wheel_id
        self.lock = threading.Lock()